package com.fulin.rhino;

import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

public class TestRhino {
	public static void main(String[] args) {
		Context cx = Context.enter();
		Scriptable scope = cx.initStandardObjects();
		File file = new File("src/js/test.js");
		File jquery = new File("src/js/jquery.3.2.1.js");
		try {
			LineNumberReader reader = new LineNumberReader(new FileReader(file));
			LineNumberReader reader2 = new LineNumberReader(new FileReader(jquery));
			
			String temp = null;
			String temp2 = null;
			StringBuffer sb = new StringBuffer();
			while((temp = reader.readLine()) != null){
				sb.append(temp).append("\n");
			}
			StringBuffer sb2 = new StringBuffer();
			while((temp2 = reader2.readLine()) != null){
				sb2.append(temp2).append("\n");
			}
			Object out = Context.javaToJS(System.out, scope); 
			ScriptableObject.putProperty(scope, "out", out); 
			
			Object result = cx.evaluateString(scope, sb.toString(), null, 1, null);
			System.out.println(cx.toString(result));
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			cx.exit();
		}
	}
}
